<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传单张图片原生</title>
</head>

<body>
<h2>方法2</h2>
<h2>http://localhost:8080/up1img2.html</h2>
<input type="file" id="file">
<button id="btn">upload</button>
</body>
<script>
    let btn = document.querySelector('#btn')

    btn.onclick = function () {
        let file = document.querySelector('#file').files[0]
        upload(file)

        function upload(file) {
            let xhr = new XMLHttpRequest()
            xhr.open('post', '/up1img', true)
            let formData = new FormData()
            formData.set('filename', file)
            xhr.send(formData)
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    alert('success')
                }
            }
        }
    }
</script>

</html>
